<?php use_stylesheet('forms.css') //load css ?>
<div id="page">
    <h1>View Sales</h1>
    <div id="form"> 
        <!-- the go back button on the side -->
        <a href="<?php echo url_for('admin_index') ?>" class="bt_back"><img src="<?php echo image_path('Buttons/bt_arrow_back.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_arrow_back_hov.png') ?>'" onmouseout="this.src='<?php echo image_path('Buttons/bt_arrow_back.png') ?>'"></a>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <fieldset>
            <table>
                <tr>
                    <td class="bold">Overview Type: </td>
                    <td><select name="overview">
                           <option value="<?php echo null ?>"></option>
                       <!-- Uncomment these to add more options-->
                       <!--<option value="Customer">Customer</option>
                           <option value="Product">Product</option>
                           <option value="Week">Week</option>
                           <option value="Month">Month</option>
                           <option value="Quarter">Quarter</option>-->
                           <option value="Year">Year</option>
                         </select>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" name="overview_button" value="Submit"/></td>
                </tr>
            </table>
        </fieldset>
        </form>
        
        <?php if(isset($_POST['overview_button']) || isset($_POST['product_submit'])){
            if(!isset($_POST['overview_button'])){ $overview = 'Product'; } else {$overview = $_POST['overview'];}
        
               //check if there are collected orders
               $collected = _OrderTable::getInstance()->findBy('status', 'collected');
               if($collected->count() != 0){
               
               //Find the oldest year something was sold and the current year
               $oldOrders = _OrderTable::findLowestDate();
               foreach($oldOrders as $oldOrder){
                   $oldDate = $oldOrder->getDateCompleted();
                   break;
               }
               $oldestYear = date("Y",  strtotime($oldDate));
               $currentYear = date("Y", time());
               ?>
        
           <?php if($overview == 'Year'){ //Show sales per Year ?>

               
            <table>
                <tr>
                    <td>Year</td>
                    <td>January</td>
                    <td>February</td>
                    <td>March</td>
                    <td>April</td>
                    <td>May</td>
                    <td>June</td>
                    <td>July</td>
                    <td>August</td>
                    <td>September</td>
                    <td>October</td>
                    <td>November</td>
                    <td>December</td>
                    <td>Total</td>
                </tr>
                    <!-- Start at the oldest year up to the current year and give the total sales of each year -->
                    <?php $year = $oldestYear;
                    while($year <= $currentYear){
                        $salesYear = _OrderTable::findTotalSalesYear($year);
                        foreach($salesYear as $sale){ ?>
                        <tr>
                            <td><?php echo $year; ?></td>
                            <?php $month = 1;
                            while($month <= 12){
                                $salesMonth = _OrderTable::findTotalSalesMonth($year, $month);
                                foreach($salesMonth as $saleMonth){ ?>
                                    <td><?php echo $saleMonth->getTotalPrice(); ?></td>
                                <?php break; }
                                $month++; } ?>
                           
                            <td><?php echo $sale->getTotalPrice(); ?></td>
                        </tr>
                            <?php break;
                        }
                        $year++;
                    } ?>
            </table>               
          <?php  
          } ?>
          
         <?php if($overview == 'Product'){ //show sales per product ?>
                <!-- show a new form to enter the product number -->
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                    <fieldset>
                        <table>
                            <tr>
                                <td class="bold">Product No.: </td>
                                <td><input type="text" name="product_no"/></td>
                            </tr>
                            <tr>
                                <td><input type="submit" name="product_submit" value="Search"/></td>
                            </tr>
                        </table>
                    </fieldset>
                </form>
                <?php if(!empty($_POST['product_no'])){
                        $productNo = $_POST['product_no'];
                        //Get the product id belonging to this number, if the number does not exist show message
                        $product = ProductTable::getInstance()->findOneBy('product_number', $productNo);
                        if($product != null){
                            $productid = $product->getId(); ?>
                            
                            <table>
                                <tr>
                                    <td>Year</td>
                                    <td>January</td>
                                    <td>February</td>
                                    <td>March</td>
                                    <td>April</td>
                                    <td>May</td>
                                    <td>June</td>
                                    <td>July</td>
                                    <td>August</td>
                                    <td>September</td>
                                    <td>October</td>
                                    <td>November</td>
                                    <td>December</td>
                                    <td>Total</td>
                                </tr>
                                     Start at the oldest year up to the current year and give the total sales of each year 
                                    <?php $year = $oldestYear;
                                    while($year <= $currentYear){
                                        $salesYear = OrderlineTable::findbyProductAndYear($productid, $year); //This query is wrong, it returns more then one record
                                        foreach($salesYear as $sale){ ?>
                                        <tr>
                                            <td><?php echo $year; ?></td>
                                            <?php $month = 1;
                                            while($month <= 12){
                                                $salesMonth = OrderlineTable::findbyProductAndYearMonth($productid, $year, $month); //This query is wrong, it returns more then one record
                                                foreach($salesMonth as $saleMonth){ ?>
                                                    <td><?php echo $saleMonth->getPrice(); ?></td>
                                                <?php break; }
                                                $month++; } ?>

                                            <td><?php echo $sale->getPrice(); ?></td>
                                        </tr>
                                            <?php break;
                                        }
                                        $year++;
                                    } ?>
                            </table> 
        
                        <?php }
                        else{
                            echo 'The entered number does not exist';
                        }
                        
                        
                      } else { echo 'Please enter a product number'; }    
               }
        } else { echo 'No orders are collected'; }
        }?>
    </div> <!-- close div form -->
</div>